package com.ruoyi.repayment.controller;


import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.repayment.pojo.*;
import com.ruoyi.repayment.service.LoanService;
import com.ruoyi.repayment.service.PrincipalService;
import com.ruoyi.repayment.service.ProductService;
import com.ruoyi.repayment.service.SuchratesService;
import com.ruoyi.repayment.utils.ResultInfo;
import com.ruoyi.repayment.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/loan/sq")
public class Loancontroller extends BaseController {
    @Autowired
    LoanService loanService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    SuchratesService suchratesService;

    @Autowired
    PrincipalService benjinService;
    //登录
    @PostMapping("/login")
    public ResultInfo login(@RequestBody User user) {
        ResultInfo info = new ResultInfo();
        if (user.getUsername() == null || user.getPassword() == null) {
            info.setFlag(false);
            info.setMessage("用户名或密码为空");
            return info;
        }
        User user2 = loanService.login(user);
        if (user2 == null) {
            info.setFlag(false);
            info.setMessage("用户名或密码错误");
            return info;
        }
        info.setFlag(true);
        info.setMessage("登录成功");
        info.setData(user2);
        return info;
    }
    //注册
    @PostMapping("/register")
    public ResultInfo register(@RequestBody User user) {
        ResultInfo info = new ResultInfo();
        if (user.getUsername() == null || user.getPassword() == null || user.getEmail() == null) {
            info.setFlag(false);
            info.setMessage("用户名或密码或邮箱为空");
            return info;
        }
        User user2 = loanService.register(user);
        if (user2 == null) {
            info.setFlag(false);
            info.setMessage("用户名或密码或邮箱已被注册");
            return info;
        }
        info.setFlag(true);
        info.setMessage("注册成功");
        info.setData(user2);
        return info;

    }

    @GetMapping("/lists")
    public ResultInfo lists(){
        List<LoanApplyfor> list=loanService.lists();
        return new ResultInfo(true, StatusCode.SUCCESS_CODE,StatusCode.SUCCESS_MEG,list);
    }

    @GetMapping("/xiCalculate/{id}/{productId}/{userId}")
    public ResultInfo calculate(@PathVariable Integer id,@PathVariable Integer productId,@PathVariable Integer userId){
        suchratesService.calculate(id,productId,userId);
        return new ResultInfo(true, StatusCode.SUCCESS_CODE,StatusCode.SUCCESS_MEG);

    }
    @GetMapping("/xiprincipal/{id}/{productId}/{userId}")
    public ResultInfo principal(@PathVariable Integer id,@PathVariable Integer productId,@PathVariable Integer userId){
        benjinService.principalll(id,productId,userId);
        return new ResultInfo(true, StatusCode.SUCCESS_CODE,StatusCode.SUCCESS_MEG);
    }

    @GetMapping("/benjinxq/{id}/{userId}")
    public ResultInfo benjinxq(@PathVariable Integer id,@PathVariable Integer userId){
        List<Benjin> list=benjinService.benjinxq(id,userId);
        return new ResultInfo(true, StatusCode.SUCCESS_CODE,StatusCode.SUCCESS_MEG,list);
    }
    @GetMapping("/Calculatexq/{id}/{userId}")
    public ResultInfo Calculatexq(@PathVariable Integer id,@PathVariable Integer userId){
        List<Benjin> list=suchratesService.Calculatexq(id,userId);
        return new ResultInfo(true, StatusCode.SUCCESS_CODE,StatusCode.SUCCESS_MEG,list);
    }
    @Autowired
    ProductService productService;
    @PostMapping("/productList")
    public ResultInfo ProductList(){
        List<LoanProduct> loanProducts=productService.ProductList();
        return new ResultInfo(true, StatusCode.SUCCESS_CODE,StatusCode.SUCCESS_MEG,loanProducts);
    }

    @PostMapping("/findById/{id}")
    public ResultInfo findById(@PathVariable Long id){
        Product p=productService.findById(id);
        return new ResultInfo(true, StatusCode.SUCCESS_CODE, StatusCode.SUCCESS_MEG,p);
    }
    //生成还款记录
    @PostMapping("/loanRepay")
    public ResultInfo loanRepay(@RequestBody LoanRepay loanRepay) {
        loanRepay.setRepayTime(new Date());
        loanRepay.setRepayId(new Date().getTime());
        loanRepay.setRepayMoney(loanRepay.getMoney());
        loanRepay.setRepayType("还款");
        loanRepay.setRepayWay("还款");
        loanRepay.setRepayAccount(loanRepay.getAccount());
        loanRepay.setRepayTime(new Date());
        loanRepay.setRepayId(new Date().getTime());
        loanRepay.setRepayMoney(loanRepay.getMoney());
        loanRepay.setRepayType("还款");
        loanRepay.setRepayWay("还款");
        loanRepay.setRepayAccount(loanRepay.getAccount());
    }
    //生成贷款额度
    @PostMapping("/loanRepayList")
    public ResultInfo loanRepayList(@RequestBody LoanRepay loanRepay) {
        loanRepay.setRepayTime(new Date());
        loanRepay.setRepayId(new Date().getTime());
        loanRepay.setRepayMoney(loanRepay.getMoney());
        loanRepay.setRepayType("还款");
        loanRepay.setRepayWay("还款");
        loanRepay.setRepayAccount(loanRepay.getAccount());

    }

  
}